package com.panshi.controller.question;

import com.panshi.api.AnswerAuditApi;
import com.panshi.api.UserApi;
import com.panshi.bean.PageBean;
import com.panshi.po.AnswerAuditPO;
import com.panshi.po.QuestionAuditPO;
import com.panshi.po.UserPO;
import com.panshi.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@Controller
@Slf4j
public class AnswerAuditController {

    @Autowired
    AnswerAuditApi answerAuditApi;

    @Autowired
    UserApi userApi;

    @GetMapping("answer-audit-home")
    public String queryHome(){
        return "answeraudit/answer-audit";
    }

    @PostMapping("answer-audit-queryAll")
    @ResponseBody
    public PageModel<AnswerAuditPO> queryAll(PageModel<AnswerAuditPO> pageModel){
        PageVO pageVO = new PageVO();
        pageVO.setSize(pageModel.getPageSize());
        pageVO.setCurrenPage(pageModel.getCurrentPage());
        List<AnswerAuditPO> list=answerAuditApi.query(pageVO).getData();
        Integer integer=answerAuditApi.totalAll().getData();
        pageModel.setList(list);
        pageModel.setCount(integer);
        return pageModel;
    }

    @PostMapping("questionAudit-likeQuery/{vague}")
    @ResponseBody
    public PageModel<AnswerAuditPO> limitQuery(@PathVariable String vague, PageModel<AnswerAuditPO> pageModel){
        log.info("进来了=================={}",vague);
        AnswerAuditDimQueryVO answerAuditDimQueryVO = new AnswerAuditDimQueryVO();
        answerAuditDimQueryVO.setSize(pageModel.getPageSize());
        answerAuditDimQueryVO.setCurrenPage(pageModel.getCurrentPage());
        answerAuditDimQueryVO.setIssue(vague);
        List<AnswerAuditPO> list =answerAuditApi.dimQuery(answerAuditDimQueryVO).getData();
        Integer integer=answerAuditApi.dimIssueTotal(vague).getData();
        pageModel.setList(list);
        pageModel.setCount(integer);
        return pageModel;
    }

    @PostMapping("questionAudit-stateQuery/{state}")
    @ResponseBody
    public PageModel<AnswerAuditPO> stateQuery(@PathVariable Integer state, PageModel<AnswerAuditPO> pageModel){
        log.info("进来了=================={}",state);
        if(state == -1){
            queryAll(pageModel);
            return pageModel;
        }
        AnswerAuditStateQueryVO answerAuditStateQueryVO = new AnswerAuditStateQueryVO();
        answerAuditStateQueryVO.setState(state);
        answerAuditStateQueryVO.setCurrenPage(pageModel.getCurrentPage());
        answerAuditStateQueryVO.setSize(pageModel.getPageSize());
        List<AnswerAuditPO> list=answerAuditApi.stateQuery(answerAuditStateQueryVO).getData();
        Integer integer=answerAuditApi.stateCount(state).getData();
        pageModel.setList(list);
        pageModel.setCount(integer);
        return pageModel;
    }


    @GetMapping("answer-audit-audit/{id}")
    @ResponseBody
    public ModelAndView queryAll(@PathVariable Integer id){
        AnswerAuditPO answerAuditPO=answerAuditApi.byIdQuery(id).getData();
        UserPO userPO =userApi.findUserById(answerAuditPO.getUid()).getData();
        ModelAndView mv = new ModelAndView("answeraudit/answer-audit-edit");
        mv.addObject("answerAuditPO",answerAuditPO);
        mv.addObject("userPO",userPO);
        return mv;
    }

    @PostMapping("answer-audit-alter")
    @ResponseBody
    public ResponseVO update(AnswerAuditUpdateVO answerAuditUpdateVO){

        answerAuditUpdateVO.setAuditdate(new Date());
        log.info("进来了=================={}",answerAuditUpdateVO);
        Integer integer=answerAuditApi.updateAnswerAudit(answerAuditUpdateVO).getData();
        log.info("integer=={}",integer);
        if(integer == 1){
            return new ResponseVO("200","修改成功",true);
        }
        return new ResponseVO("500","修改失败",false);
    }


}
